App.controller('busPublishTasksController', ['$rootScope', '$scope', '$state', 'Page', function($rootScope, $scope, $state, Page) {
	$scope.orderType = '1';
	$scope.workbillType = '2';
	
	Page.ajaxPostQuiet('employeeGroup/groupList.do', null, function (resp) {
        $scope.groupList = resp.data;
	});
	
    $scope.grid = {
        column_defs: [
         {
            target : 14,
            buttons : [
                {
                    name: '办理',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    visiable: function(row) {
                        return row.status && row.status == '1';
                    },
                    event: function(row){
                    	$scope.orderItem = angular.copy(row);
                    	$scope.task = row;
                    	$($scope.groupList).each(function(i,g){
                    		if(g.groupId == row.workGroupId){
                    			$scope.emps = g.members;
                    		}
                    	});
                    	if(!$scope.task.groupTasks){
                    		Page.ajaxPostQuiet('workbill/task/tasksOfOrderItem.do', {orderItemId : row.orderItemId}, function (resp) {
                        		$scope.task.groupTasks = resp.data;
                        		$scope.taskDealModal.show();
                        	});
                    	}else{
                    		$scope.taskDealModal.show();
                    	}
                    }
                },
                {
                    name: '查看',
                    class: 'btn-warning btm-sm',
                    icon: 'fa-edit',
                    visiable: function(row) {
                        return row.status && row.status == '2';
                    },
                    event: function(row){
                    	$scope.orderItem = angular.copy(row);
                    	$scope.task = row;
                    	$($scope.groupList).each(function(i,g){
                    		if(g.groupId == row.workGroupId){
                    			$scope.emps = g.members;
                    		}
                    	});
                    	if(!$scope.task.groupTasks){
                    		Page.ajaxPostQuiet('workbill/task/tasksOfOrderItem.do', {orderItemId : row.orderItemId}, function (resp) {
                        		$scope.task.groupTasks = resp.data;
                        		$scope.taskViewModal.show();
                        	});
                    	}else{
                    		$scope.taskViewModal.show();
                    	}
                    }
                }
            ]
        }],
        removeImg : function(img){
        	for(var i = 0 ;i < $scope.task.files.length; i++){
        		if($scope.task.files[i].path == img.path){
        			$scope.task.files.splice(i,1);
        			return;
        		}
        	}
        },
        submitTask : function(){
        	$scope.task.empIds = [];
        	$($scope.emps).each(function(i,e){
        		if(e.selected){
        			$scope.task.empIds.push(e.empId);
        		}
        	});
        	if($scope.task.empIds.length == 0){
        		Page.alert('请选择组员!');
        	}
        	Page.ajaxPostQuiet('workbill/task/complete.do', $scope.task, function (resp) {
        		Page.alert('任务提交成功');
        		$scope.grid.query();
        		$scope.taskDealModal.hide();
        	});
        },
        cancel : function(){
        	$scope.taskDealModal.hide();
        }
    };
    
    


}]);